/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javagameengine;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import javax.imageio.ImageIO;

/**
 *
 * @author jimi
 */
public class Still implements Anim
{

	public boolean is_visible = true, is_active = true;
	public Point position = new Point();
	protected String name;
	protected BufferedImage image;
	boolean live = false;

	static class HashTable
	{

		static HashMap map = new HashMap<>();

		public static BufferedImage add( String name )
		{
			if( !map.containsKey( name ) )
			{
				BufferedImage ret = null;

				try
				{
					Class cl = name.getClass();//.getClassLoader();
					URL url ;
					if( cl == null )
					{
						System.out.println( "Issue in class loader" );
					}
					url = cl.getResource( name );
					if( url == null )
					{
						System.out.println( "Issue is in getResource" );
					}
					if( url != null )
						ret = ImageIO.read( url );
				}
				catch( IOException e )
				{
					System.out.println( "FATAL ERROR:" );
					System.out.println( e.getMessage() );
					System.out.println( "Exiting..." );

					System.exit( -1 );
				}
				map.put( name, ret );
				return ret;
			}



			return (BufferedImage)map.get( name );
		}
	}

	@Override
	public void load( String path )
	{
		if( live )
		{
			flush();
		}

		name = path;

		image = HashTable.add( path );
		live = true;
	}

	@Override
	public void flush()
	{
		name = "";
		image = null;
		live = false;
	}

	@Override
	public void update( double delta_t )
	{
		// nothing to do here
	}

	@Override
	public void draw( Graphics2D g, Matrix transform )
	{
		if( is_active && is_visible && live )
		{
			Matrix temp = Matrix.translation( position.to_vector() ).times( transform );
			g.drawImage( image, temp.to_affine(), null );
		}
	}
}
